package com.appspot.zarm.gomoku.action;

import com.appspot.zarm.controller.GameAction;
import com.appspot.zarm.game.GamesStorage;
import com.appspot.zarm.game.IUser;
import com.appspot.zarm.game.Player;
import com.appspot.zarm.game.PlayerType;
import com.appspot.zarm.game.User;
import com.appspot.zarm.gomoku.Coord;
import com.appspot.zarm.gomoku.GomokuGame;
import com.appspot.zarm.gomoku.GomokuGameFacade;
import com.appspot.zarm.gomoku.transfer.BoardExternalizer;
import com.appspot.zarm.gomoku.transfer.JsonTransfer;

public class GomokuAction implements GameAction {

	public static final String TURN_ACTION = "turn";
	public static final String START_ACTION = "start";
	public static final String FINISH_ACTION = "finish";
	public static final String JOIN_ACTION = "join";
	public static final String REFRESH_ACTION = "refresh";
	public static final String GET_GAMES_LIST = "getGamesList";

	@Override
	public String process(GamesStorage gamesStorage, IUser user, String action, String data, String message, boolean isMultiplayer, String joinId) {

		if (action.equals(GET_GAMES_LIST)) {
			return JsonTransfer.getGamesListToJson(gamesStorage);
		}
		
		GomokuGame game;
		if (joinId != null) {
			game = (GomokuGame)gamesStorage.getGame(new User(joinId));	// get game by sessionId of the first player
		} else {
			game = (GomokuGame)gamesStorage.getGame(user);
		}

		/////////////////////////////////////////////////////////////////////////////
		// Game not exists yet													   //
		/////////////////////////////////////////////////////////////////////////////
		if (game == null) {
			if (action.equals(START_ACTION)) {
				Player player = new Player(user, PlayerType.ONE);
				GomokuGame newGame = new GomokuGame(player);
				gamesStorage.addGame(newGame);
				
				if (isMultiplayer) {
					// for multiplayer second player is null
					
				} else {
					
					// for single player just add the second player
					Player computerPlayer = new Player(user, PlayerType.TWO, true);
					newGame.addPlayer(computerPlayer);
				}
			}

		/////////////////////////////////////////////////////////////////////////////
		// Game exists															   //
		/////////////////////////////////////////////////////////////////////////////
		} else {

			if (action.equals(START_ACTION) && joinId != null) {

				Player playerOne = game.getPlayerOne();
				Player playerTwo = new Player(user, PlayerType.TWO);
				game.addPlayer(playerTwo);

				// set rival turn
				if (!game.isThisPlayerTurn(playerOne)) {
					game.setThisPlayerTurn(playerTwo);
				}

				return JsonTransfer.getBoardToJson((BoardExternalizer) game.getBoard(), "Ok");

			} else if (action.equals(FINISH_ACTION)) {
				
				gamesStorage.removeGame(user);

			} else if (action.equals(TURN_ACTION)) {

				GomokuGameFacade gomokuGameFacade = new GomokuGameFacade(game, user);
				Coord position = JsonTransfer.getCoordFromJson(data);
				gomokuGameFacade.doTurn(position);

				return JsonTransfer.getBoardToJson((BoardExternalizer) game.getBoard(), message);

			} else if (action.equals(REFRESH_ACTION)) {

				return JsonTransfer.getBoardToJson((BoardExternalizer) game.getBoard(), message);

			}
		}

		return null;
	}

}
